//
//  AppDelegate+WechatPayService.m
//  V-Learn
//
//  Created by Apple on 2019/1/17.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "AppDelegate+WechatPayService.h"
//#import <WechatOpenSDK/WXApi.h> // 引入微信 SDK
//#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#import "WXAuth.h"

@interface AppDelegate () <WXApiDelegate> // 遵守 SDK 协议

@end

@implementation AppDelegate (WechatPayService)
#pragma mark - Public

- (void)hql_configureForWechatPay {
    //向微信注册
    //注：WechatAppId 是常量，已经写在了预编译配置文件中
    //#define WechatAppId @"wx12345...67890"
    [WXApi registerApp:WechatAppId];
}

#pragma mark - WeChatPay

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//            NSLog(@"result = %@",resultDic);
//            [[NSNotificationCenter defaultCenter] postNotificationName:LBZhifubaoPayOnResponceNotification object:resultDic];
////            NSInteger orderState = [resultDic[@"resultStatus"] integerValue];
////            if (orderState==9000) {
////                NSLog(@"订单支付成功");
//
////            }else if (orderState==8000){
////                NSLog(@"订单支付成功");
////            }else if (orderState==4000){
////                NSLog(@"订单支付失败");
////                [LBToast showWithText:@"订单支付失败"];
////            }else if (orderState==6001){
////                NSLog(@"用户中途取消");
////            }else if (orderState==6002){
////                NSLog(@"网络连接出错");
////            }
//
//        }];
        
        // 授权跳转支付宝钱包进行支付，处理支付结果
//        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
//            // 解析 auth code
//            NSString *result = resultDic[@"result"];
//            NSString *authCode = nil;
//            if (result.length>0) {
//                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
//                for (NSString *subResult in resultArr) {
//                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
//                        authCode = [subResult substringFromIndex:10];
//                        break;
//                    }
//                }
//            }
////            NSLog(@"授权结果 authCode = %@", authCode?:@"");
//        }];

    }else if([url.host isEqualToString:@"pay"]){

        return  [WXApi handleOpenURL:url delegate:self];
    }else{
        return [WXAUTH handleOpenURL:url];
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [WXApi handleOpenURL:url delegate:self];
    });
//    return [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WXApi handleOpenURL:url delegate:self];
}

/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容，是自动释放的
 */
-(void) onResp:(BaseResp*)resp {
    // 微信支付成功/失败，发起通知查询
    // 注：HQLWechatPayOnResponceNotification 通知方法名是常量，统一写在配置文件中
    // #define HQLWechatPayOnResponceNotification @"HQLWechatPayOnResponceNotification"
    [[NSNotificationCenter defaultCenter] postNotificationName:LBWechatPayOnResponceNotification
                                                        object:(BaseReq *)resp];
}

@end
